package com.xhsj.user.sms.purchase.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.xhsj.user.sms.purchase.entity.TbPurchaseProject;
import com.xhsj.user.sms.purchase.entity.TbPurchaseUserProject;
import com.xhsj.user.sms.purchase.mapper.TbPurchaseProjectMapper;
import com.xhsj.user.sms.purchase.service.TbPurchaseProjectService;
import com.xhsj.user.sms.purchase.service.TbPurchaseUserProjectService;
import com.xhsj.user.utils.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * 采购项目 前端控制器
 * </p>
 *
 * @author liuqinghua
 * @since 2020-06-11
 */
@Slf4j
@Api(tags = "采购项目")
@RestController
@RequestMapping("/purchase/tbPurchaseProject")
public class TbPurchaseProjectController {

	@Autowired
    public TbPurchaseProjectService service;
	@Autowired
    public TbPurchaseProjectMapper tbPurchaseProjectMapper;

	@Autowired
	private TbPurchaseUserProjectService tbPurchaseUserProjectService;


	@ApiOperation(value = "采购项目,保存/修改", notes = "采购项目,保存和修改方法")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public Message save(@RequestBody TbPurchaseProject data){
    	try {
			service.saveProject(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
        	return Message.error(1,"失败");
		}

	}

	@ApiOperation(value = "查询所有项目", notes = "查询所有项目")
	@RequestMapping(value = "/getPurchaseProject", method = RequestMethod.GET)
	public Message getPurchaseProject() {

		try {
			QueryWrapper<TbPurchaseProject> queryWrapper = new QueryWrapper<>();
			queryWrapper.select("id AS value","project_name AS label");

			List<Map<String, Object>> maps = tbPurchaseProjectMapper.selectMaps(queryWrapper);

			return Message.success("成功",maps);
		} catch (Exception e) {
			log.error("保存失败:{}",e);
			return Message.error(1,"失败");
		}

	}


	@ApiOperation(value = "采购项目删除", notes = "通过id删除采购项目信息。")
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public Message delete(@RequestParam(value="id") String id) {
		try {
			service.removeById(id);
        	return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
            return Message.error(1,"失败");
		}

	}

	@ApiOperation(value = "批量删除", notes = "批量删除。")
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	public Message batchDelete(@RequestBody List<String> list) {
		try {
			service.removeByIds(list);
    		return Message.success("成功");
		} catch (Exception e) {
    		log.error("删除失败:{}",e);
    		return Message.error(1,"失败");
		}
    }

	@ApiOperation(value = "分页查询采购项目list", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
	@RequestMapping(value="/findList",method = RequestMethod.POST)
    //@PreAuthorize("hasAuthority('tb:purchase:project:view')")
	public Message findList(@RequestBody Map<String,Object> data) {
        try {
        	IPage<TbPurchaseProject> selectPage = service.selectPage(data);
    		return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
        } catch (Exception e) {
			log.error("查询异常:{}",e);
    		return Message.error(1,"失败");
        }
	}

	@ApiOperation(value = "查询采购项目所有数据list", notes = "查询采购项目所有数据list。")
	@RequestMapping(value="/findAll",method = RequestMethod.GET)
	public Message findAll(){
		return Message.success("成功",service.selectAll());
	}


	@ApiOperation(value = "通过id获取采购项目实体类", notes = "通过id获取采购项目实体类。")
	@RequestMapping(value="/getById",method = RequestMethod.GET)
    //@PreAuthorize("hasAuthority('tb:purchase:project:view')")
	public Message getById(@RequestParam(value="id") String id) {
    	return Message.success("成功",service.getById(id));
	}

	@ApiOperation(value = "通过id获取实体类详细信息", notes = "通过id获取实体类详细信息。")
	@RequestMapping(value="/detail",method = RequestMethod.GET)
    //@PreAuthorize("hasAuthority('tb:purchase:project:view')")
	public Message detail(@RequestParam(value="id") String id) {
    	return Message.success("成功",service.getById(id));
	}


	@ApiOperation(value="通过id查询项目成员",notes ="通过id查询项目成员" )
	@RequestMapping(value = "/getProjectUserById",method = RequestMethod.GET)
	public Message getProjectUserById(@RequestParam("id") String id){
		try {
			//项目数据
			TbPurchaseProject byId = service.getById(id);
            //项目与用户数据
			QueryWrapper<TbPurchaseUserProject> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("project_id",byId.getId());
			queryWrapper.orderByAsc("dept_id,user_id");
			List<TbPurchaseUserProject> userProjectList= tbPurchaseUserProjectService.list(queryWrapper);
			List<Object> re = Lists.newArrayList();
			for (TbPurchaseUserProject userProject : userProjectList) {
				List<Object> list = Lists.newArrayList();
				list.add(userProject.getDeptId());
				list.add(userProject.getUserId());
				re.add(list);
			}
			byId.setUserList(re);
			return Message.success("",byId);
		} catch (Exception e) {
			e.printStackTrace();
			return Message.error();
		}
	}

}

